// dom function
/*
 document.body.appendChild(
 dom("P", null, "A paragraph with a ",
 dom("A", {href: "http://en.wikipedia.org/wiki/Alchemy"}, "link"),
 " inside of it."));
 */
function dom(name, attributes) {
    var node = document.createElement(name);
    if (attributes) {
        forEachIn(attributes, function(name, value) {
            node.setAttribute(name, value);
        });
    }
    for (var i = 2; i < arguments.length; i++) {
        var child = arguments[i];
        if (typeof child == "string")
            child = document.createTextNode(child);
        node.appendChild(child);
    }
    return node;
}

function forEachIn(object, action) {
    for (var property in object) {
        //if(object.hasOwnProperty(property)) 与下面一句话等同
        if (Object.prototype.hasOwnProperty.call(object, property))
            action(property, object[property]);
    }
}

function registerEventHandler(node,event,handler){

    if(typeof(node.addEventListener)=="function"){
        node.addEventListener(event,handler,false);
    }else{
        node.attachEvent("on"+event,handler);
    }
}

//把func和object绑定到一起，这样func内的this指的就是object.
/*
 var x = [];
 var pushX = bind(x.push, x);
 pushX("A");
 pushX("B");
 console.log(x);
 */
function bind(func, object) {
    return function(){
        return func.apply(object, arguments);
    };
}

//调用object的name方法，并把object当作该方法的this对象。
//var pushX = method(x, "push");
function method(object, name) {
    return function() {
        object[name].apply(object, arguments);
    };
}







